package cn.bitkit.base.util;

import java.util.concurrent.ThreadLocalRandom;

/**
 * 
 * @author changbo
 * 
 */
public class RandomUtil{
	
	private static final char[] NUM_STR = "0123456789".toCharArray();
	private static final char[] HEX_STR = "0123456789abcdef".toCharArray();
	private static final char[] WORD_STR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
	private static final char[] CAPTCHA_STR = "0123456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ".toCharArray();

	public static ThreadLocalRandom getRandom() {
		return ThreadLocalRandom.current();
	}
	
	public static String random(char[] str, int length){
		StringBuilder sb = new StringBuilder();
        getRandom().ints(length).forEach(i -> sb.append(str[Math.abs(i)%str.length]));
		return sb.toString();
	}

	public static String hex32() {
		return random(HEX_STR, 32);
	}
	
	public static String hex(int length){
		return random(HEX_STR, length);
	}

	public static String number(int length){
		return random(NUM_STR, length);
	}
	
	public static String word(int length){
		return random(WORD_STR, length);
	}
	
	public static String captcha(int length){
		return random(CAPTCHA_STR, length);
	}
	
	public static int random(int start, int end){
		return getRandom().nextInt(start, end);
	}
	
}
